package cn.lf.android.lawfirm.undertake.approval;


import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import cn.lf.android.lawfirm.OrdinaryBaseActivity;
import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.distribution.modle.LawCase;
import cn.lf.android.lawfirm.undertake.approval.controler.UnderListConroller;
import cn.lf.android.lawfirm.undertake.approval.view.UndertakeAdapter;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.SPUtil;

import static cn.lf.android.lawfirm.MyApplication.context;
import static cn.lf.android.lawfirm.R.id.img_left;
import static cn.lf.android.lawfirm.R.id.notYet;
import static cn.lf.android.lawfirm.R.id.pass;
import static cn.lf.android.lawfirm.R.id.rl_search;
import static cn.lf.android.lawfirm.R.id.sendBack;

public class UndertakeListActivity extends OrdinaryBaseActivity
        implements UnderListConroller.UnderListCallback {
    private ListView mListView;
    private SmartRefreshLayout refreshLayout;
    private UndertakeAdapter mUndertakeAdapter;
    private List<LawCase> list = new ArrayList<LawCase>();

    private UnderListConroller mUnderListConroller;
    private int pageIndex = 1;
    /**
     * 待审批（notYet）、已审批（pass）、已退回（sendBack）和全部（all）（必填默认为待审批）（必填）
     **/
    private String state = "notYet";
    private TextView mNotYet, mPass, mSendBack;
    private LinearLayout ll_select;
    public static Boolean isRefresh = false;

    @Override
    public int getChildView() {
        return R.layout.activity_undertake_list;
    }

    @Override
    public void initViewId() {
        initTitle();
        refreshLayout = (SmartRefreshLayout) findId(R.id.refreshLayout);
        mListView = (ListView) findId(R.id.lv_list);
        mUndertakeAdapter = new UndertakeAdapter(this, list, 0);
        mListView.setAdapter(mUndertakeAdapter);
        tvSearch.setVisibility(View.VISIBLE);
        mSearch.setVisibility(View.GONE);
        mSearch.setVisibility(View.GONE);
        mNotYet = (TextView) findId(notYet);
        mNotYet.setOnClickListener(this);
        mPass = (TextView) findId(pass);
        mPass.setOnClickListener(this);
        mSendBack = (TextView) findId(sendBack);
        mSendBack.setOnClickListener(this);
        ll_select = (LinearLayout) findId(R.id.ll_select);
        ll_select.setVisibility(View.VISIBLE);
        setLisenters();
    }

    @Override
    public void refreshLoad() {

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case notYet://待审批
                state = "notYet";
                mNotYet.setTextColor(0xffff6468);
                mPass.setTextColor(0xff666666);
                mSendBack.setTextColor(0xff666666);
                refreshLayout.autoRefresh();
                break;
            case sendBack://已退回
                state = "sendBack";
                mNotYet.setTextColor(0xff666666);
                mPass.setTextColor(0xff666666);
                mSendBack.setTextColor(0xffff6468);
                refreshLayout.autoRefresh();
                break;
            case pass://已审批
                state = "pass";
                mNotYet.setTextColor(0xff666666);
                mPass.setTextColor(0xffff6468);
                mSendBack.setTextColor(0xff666666);
                refreshLayout.autoRefresh();
                break;
            case img_left:
                finish();
                break;
            case rl_search:
                Intent intent = new Intent(this, UndertakeSearchListActivity.class);
                intent.putExtra("state",state);
                startActivity(intent);
                break;
            default:
                break;
        }
    }


    private void initTitle() {
        mImgLeft.setVisibility(View.VISIBLE);
        mImgLeft.setOnClickListener(this);
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setText("承办审批");
    }

    private void setLisenters() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(0);//0秒
                pageIndex = 1;
                list.clear();
                mUnderListConroller.onUnderList("", String.valueOf(pageIndex), state);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(0);
                pageIndex++;
                mUnderListConroller.onUnderList("", String.valueOf(pageIndex), state);
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Intent intent = new Intent(UndertakeListActivity.this, CaseDetailActivity.class);
//                intent.putExtra("caseID", list.get(position).caseID);
//                startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {
        getData();
        SPUtil spUtil = new SPUtil(context);
        DebugLog.e("Authorization---" + spUtil.getAccessToken());
        mUnderListConroller = new UnderListConroller(this, this);
        mUnderListConroller.onUnderList("", String.valueOf(pageIndex), state);
    }


    private void getData() {
    }

    @Override
    public void onUnderListuccessed(List<LawCase> beans) {
        list.addAll(beans);
        mUndertakeAdapter.notifyDataSetChanged();
        if (list.size() == 0) {
            noData2.setVisibility(View.VISIBLE);
        } else {
            noData2.setVisibility(View.GONE);
        }
    }

    @Override
    public void onUnderListFailed() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isRefresh) {
            if (null != refreshLayout) {
                refreshLayout.autoRefresh();
                isRefresh = false;
            }
        }
    }
}

